<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket demo</title>
</head>
<body>
<button id="send-button" type="button">send a message to server</button>
<script type="text/javascript">
    let webSocketClient = {
        socket: null,
        init: function () {
            if (window.WebSocket) {
                webSocketClient.socket = new WebSocket("ws://127.0.0.1:8080/simple/websocket");
                webSocketClient.socket.onopen = function () {
                    console.log("successful connection...");
                };
                webSocketClient.socket.onclose = function () {
                    console.log("connection closure...");
                };
                webSocketClient.socket.onerror = function () {
                    console.log("an error has occured...");
                };
                webSocketClient.socket.onmessage = function (resp) {
                    console.log("message received, message = " + resp.data);
                };
            } else {
                alert("the browser does not support websocket protocol");
            }
        }
    };
    webSocketClient.init();

    document.getElementById('send-button').addEventListener("click", function () {
        webSocketClient.socket.send("client send a message to server");
    });
</script>
</body>
</html>